<?php
	include ('../defines.php');
	
	FunctionsHelper::includeFiles(
		array(
			ADMINISTRATOR_CONTROLLER_DIRECTORY . 'users.php',
			ADMINISTRATOR_CONTROLLER_DIRECTORY . 'news.php'
		)
	);

	$users = new Users();
	if (!$users->isLoggedUser())
		header('Location:' . ADMINISTRATOR_VIEW_PATH . 'login.php');
		
	$newsC = new NewsC();
	$arrNews = $newsC->getAll();
	
	FunctionsHelper::includeTop();

?>

<div class="withBorder" id="main">
	<script type="text/javascript">
	$(document).ready(function(){
		$('.table').dataTable({
			"aoColumns": [
				null,
				null,
				null,
				null,
				{ "bSortable": false },
				{ "bSortable": false },
				{ "bSortable": false }
			]
		});
		
		$('.delete').live('click', function(){
			varUrl = '<?=ADMINISTRATOR_CONTROLLER_PATH; ?>operations.php';
			varData = { 'id' : this.id, 'operation' : 'delete', 'controller' : 'news' };
			callAjax(varUrl, 'post', varData, funcSuccess);
		});
		$('.activate').live('click', function(){
			varUrl = '<?=ADMINISTRATOR_CONTROLLER_PATH; ?>operations.php';
			varData = { 'id' : this.id, 'operation' : 'activate', 'controller' : 'news' };
			callAjax(varUrl, 'post', varData, funcSuccess);
		});
		$('.edit').live('click', function(){
			document.location.href='<?=ADMINISTRATOR_VIEW_PATH;?>news_am.php?id='+ this.id+'&action=m';
		});
		$('.create').click(function(){
			document.location.href='<?=ADMINISTRATOR_VIEW_PATH;?>news_am.php?action=a';
		});
		$('#back').click(function(){
			document.location.href='<?=ADMINISTRATOR_VIEW_PATH;?>home.php';
		});
	});
	</script>
	<div id="buttonAdd" class="buttonOperation create">
		<img title="Agregar" width='30' height='30' src="<?=ADMINISTRATOR_IMAGE_PATH . 'add.png';?>" />
		<br />
		<span>NUEVO</span>
	</div>
	<?php 
		if (!empty($arrNews)){
	?>
	<div class="tableContainer">
		<table class='table' id='tblUsers'>
			<thead>
			<tr>
				<th>Tipo</th>
				<th>T&iacute;tulo</th>
				<th>Fecha</th>
				<th>Autor</th>
				<th class="tinyTable">Activo</th>
				<th class="tinyTable">Modificar</th>
				<th class="mediumTable">Activar/Desactivar</th>
			</tr>
			</thead>
			<tbody>
			<?php
			$cont=0;
			foreach ($arrNews as $news){
				$cont++;
	
				if ($cont%2==0)
					$class='even';
				else
					$class='odd';
			?>
			<tr>
				<td class='<?= $class?>'><?= $news->getType(); ?></td>
				<td class='<?= $class?>'><?= FunctionsHelper::limitText($news->getTitle(), MAX_CHARACTERS_TO_SHOW); ?></td>
				<td class='<?= $class?>'><?= date('d/m/Y', strtotime($news->getCreated())); ?></td>
				<td class='<?= $class?>'><?= utf8_encode($news->getUserId()->getName() . ' ' . $news->getUserId()->getSurname()); ?></td>
				<td class='<?= $class?>'><? echo $news->isActive() ? 'S&iacute;' : 'No' ; ?></td>
				<td class='<?= $class?>'><a href='#' class='edit buttonOperation' id='<?= $news->getNewsId(); ?>'><img title="Modificar" width='30' height='30' src="<?=ADMINISTRATOR_IMAGE_PATH . 'lapiz.png';?>" /></a></td>
				<td class='<?= $class?>'>
				<?
				if ($news->isActive()){
				?>
					<a href='#' class='delete buttonOperation' id='<?= $news->getNewsId(); ?>'><img title="Desactivar" width='30' height='30' src="<?=ADMINISTRATOR_IMAGE_PATH . 'trash.png';?>" /></a>
				<?php 
				}else{
				?>
					<a href='#' class='activate buttonOperation' id='<?= $news->getNewsId(); ?>'><img title="Activar" width='30' height='30' src="<?=ADMINISTRATOR_IMAGE_PATH . 'active.png';?>" /></a>
				<?	
				}
				?>
				</td>
			</tr>
			<?php
			}
			?>
			</tbody>
		</table>
	</div>
	<?php 
		}else{
	?>
	<div class='messageEmptyRows'><?php echo Messages::getMessage('EmptyRows');?></div>
	<?php 		
		}
	?>
	<div class="clear"></div>
	<br />
	<img class="back backTable" src='<?= ADMINISTRATOR_IMAGE_PATH . 'back.png'; ?>' id="back" width='64' height='52' />
	<div class="clear"></div>
</div>
<?php 
	FunctionsHelper::includeBottom();
?>